<?php
class Cms_Model_Article
{
	private static $_instance;
	private static $_cacheTimeout = 300;
	
	private function __construct() {
	}
	
	/**
	 * @return Cms_Model_Article
	 * */
	public static function factory() {
		if(self::$_instance == null) {
			self::$_instance = new self();
		}
		
		return self::$_instance;
	}
	
	/**
	 * Get list of Article
	 * @param array $options
	 * @return array
	 */
	public function getArticles($options = array()) {
		$articleDao = Cms_Model_DAO_Article::factory();
		try {
			$result = $articleDao->getArticles($options);
		} catch (Exception $exc) {
		    prBinh($exc);
			Utils_Global::storeLog($exc, __FILE__, __LINE__ );
		}
		
		return $result;
	}
	
	/**
	 * Get list of Workshop
	 * @param array $options
	 * @return array
	 */
	public function getWorkshops($options = array()) {
	    $articleDao = Cms_Model_DAO_Article::factory();
	    try {
	    	$result = $articleDao->getWorkshops($options);
	    } catch (Exception $exc) {
	    	prBinh($exc);
	    	Utils_Global::storeLog($exc, __FILE__, __LINE__ );
	    }
	    
	    return $result;
	}
	
	public function getDetailArticle($options = array()) {
	    $articleDao = Cms_Model_DAO_Article::factory();
	    try {
	    	$result = $articleDao->getDetailArticle($options);
	    } catch (Exception $exc) {;
	        prBinh($exc);
	    	Utils_Global::storeLog($exc, __FILE__, __LINE__ );
	    }
	    
	    return $result;
	}
	
	/**
	 * Get article count for given options
	 * @param array $options
	 * @return integer
	 * */
	public function getArticleCount($options = array()) {
		//Get from cache first
		$articleDao = Cms_Model_DAO_Article::factory();
		try {
			$result = $articleDao->getArticleCount($options);
		} catch (Exception $exc) {
		    prBinh($exc);
			Utils_Global::storeLog($exc, __FILE__, __LINE__);
		}
		
		return $result;
	}
	
	/**
	 * Get article by given category and order by id
	 * @param integer $category
	 * @param integer $offset
	 * @param integer $limit
	 * @return array
	 * */
	public function getRelatedArticle($category, $offset = 0, $limit = 3) {
		$result = array();
		if(!$category) {
			return $result;
		}
		
		//Get from cache first
		$cache = Utils_Global::getCacheInstance('cms');
		$keyConfig = Utils_Global::getConfig('cache', 'keys', 'keys');
		$key = $keyConfig->article_select . '_' . $id;
		if(is_object($cache)) {
			$result = $cache->getCache($key);
			if($result) {
				return $result;
			}
		}
		
		$options = array('category' => $category, 'offset' => $offset, 'limit' => $limit, 'order' => 'id', 'by' => 'DESC');
		$articleDao = Cms_Model_DAO_Article::factory();
		try {
			$result = $articleDao->getArticles($options);
			if($result && is_object($cache)) {
				$cache->setCache($key, $result);
			}
		} catch (Exception $exc) {
			Utils_Global::storeLog($exc, __FILE__, __LINE__);
		}
		
		return $result;
	}
	
	/**
	 * Get newest article
	 * @param integer $offset
	 * @param integer $limit
	 * @return array
	 * */
	public function getNewestArticle($offset = 0, $limit = 10) {
		$result = array();
	
		//Get from cache first
		$cache = Utils_Global::getCacheInstance('cms');
		$keyConfig = Utils_Global::getConfig('cache', 'keys', 'keys');
		$key = $keyConfig->article_select . '_newest_' . $offset . $limit;
		if(is_object($cache)) {
			$result = $cache->getCache($key);
			if($result) {
				return $result;
			}
		}
		$options = array('offset' => $offset, 'limit' => $limit, 'order' => 'id', 'by' => 'DESC');
		$articleDao = Cms_Model_DAO_Article::factory();
		try {
			$result = $articleDao->getArticles($options);
			if($result && is_object($cache)) {
				$cache->setCache($key, $result);
			}
		} catch (Exception $exc) {
			Utils_Global::storeLog($exc, __FILE__, __LINE__);
		}
		
		return $result;
	}
	
	public function getArticleByCategory($offset = 0, $limit = 16, $category, $lang) {
	    $options = array('offset' => $offset, 'limit' => $limit, 'category' => $category, 'lang' => $lang, 
	                        'order' => 'order', 'by' => 'DESC');
	    $articleDao = Cms_Model_DAO_Article::factory();
	    try {
	    	$result = $articleDao->getArticles($options);
	    } catch (Exception $exc) {
	        prBinh($exc);
	    	Utils_Global::storeLog($exc, __FILE__, __LINE__);
	    }
	    
	    return $result;
	}
	
	/**
	 * Get article by given id
	 * @param integer $id
	 * @return array
	 * */
	public function getArticleById($id) {
		$result = array();
		if(!$id) {
			return $result;
		}
		
		//Get from cache first
		$cache = Utils_Global::getCacheInstance('cms');
		$keyConfig = Utils_Global::getConfig('cache', 'keys', 'keys');
		$key = $keyConfig->article_select . '_' . $id;
		if(is_object($cache)) {
			$result = $cache->getCache($key);
			if($result) {
				return $result;
			}
		}
		
		$articleDao = Cms_Model_DAO_Article::factory();
		$options = array('id' => $id);
		try {
			$result = $articleDao->getArticles($options);
			if($result && is_object($cache)) {
				$cache->$cache->setCache($key, $result);
			}
		}catch (Exception $exc) {
			Utils_Global::storeLog($exc, __FILE__, __LINE__);
		}
		return $result;
	}
	
	/**
	 * Update article by given data
	 * @param integer $id
	 * @param array $data
	 * @return 1 on success, 0 on failure
	 * */
	public function update($id, $data) {
	    if(!$id || !$data) {
	        return 0;;
	    }
	    $articleDao = Cms_Model_DAO_Article::factory();
	    try {
	        $reuslt = $articleDao->update($id, $data);
	    } catch (Exception $exc) {
	        prBinh($exc);
	        Utils_Global::storeLog($exc, __FILE__, __LINE__);
	    }
	    
	    return $result;
	}
	
	/**
	 * +1 view count by given id
	 * @param integer $id
	 * @return 0 on success, 1 on failure
	 * */
	public function addViewCount($id) {
	    if(!$id) {
	        return 0;
	    }
	    $articleDao = Cms_Model_DAO_Article::factory();
	    try {
	        $result = $articleDao->addViewCount($id);
	    } catch (Exception $e) {
	        prBinh($e);
	        Utils_Global::storeLog($e, __FILE__, __LINE__);
	    }
	    
	    return $result;
	}
}